﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Xml;
using Zeny.Common.Utils;
using DevComponents.DotNetBar;
using System.Windows.Forms;

namespace Zeny.UI.helper
{
    class MenuItemEntity
    {
        public string formClass { get; set; }
        public string formCaption { get; set; }

        public Boolean isFormAction()
        {
            return (formClass != null) && (formCaption != null);
        }
    }

    class MenuHelper
    {
        public static void generateMenu(Bar mainmenu)
        {
            Assembly assembly;
            StreamReader streamReader;

            try
            {
                assembly = Assembly.GetExecutingAssembly();
                streamReader = new StreamReader(assembly.GetManifestResourceStream("Zeny.UI.config.menu_zh_CN.xml"));

                XmlNodeList xmlMenuList = XmlUtils.ReadMultiNode(streamReader, "/menus/menu");
                foreach (XmlNode xmlMenu in xmlMenuList)
                {
                    String menuText = XmlUtils.getSubNodeInnerText(xmlMenu, "menuText");

                    ButtonItem menuItem = new DevComponents.DotNetBar.ButtonItem();
                    mainmenu.Items.AddRange(new DevComponents.DotNetBar.BaseItem[] { menuItem });
                    menuItem.Text = menuText;

                    generateSubMenu(xmlMenu, menuItem);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.StackTrace);
                MessageBox.Show("Error accessing resources!\r\n" + ex.Message + "\r\n" + ex.StackTrace);
            }
        }

        private static void generateSubMenu(XmlNode xmlMenu, ButtonItem menuItem)
        {
            XmlNodeList xmlSubMenuList = xmlMenu.SelectNodes("menu");
            foreach (XmlNode xmlSubMenu in xmlSubMenuList)
            {
                String subMenuText = XmlUtils.getSubNodeInnerText(xmlSubMenu, "menuText");
                String subFormClass = XmlUtils.getSubNodeInnerText(xmlSubMenu, "formClass");
                String subFormCaption = XmlUtils.getSubNodeInnerText(xmlSubMenu, "formCaption");

                ButtonItem subMenuItem = new DevComponents.DotNetBar.ButtonItem();
                menuItem.SubItems.AddRange(new DevComponents.DotNetBar.BaseItem[] { subMenuItem });
                subMenuItem.Text = subMenuText;

                MenuItemEntity menuItemObject = new MenuItemEntity();
                menuItemObject.formClass = subFormClass;
                menuItemObject.formCaption = subFormCaption != null ? subFormCaption : subMenuText;
                subMenuItem.Tag = menuItemObject;

                generateSubMenu(xmlSubMenu, subMenuItem);
            }
        }
    }
}
